package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.DeleteDishException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
	@Autowired
	private DishMapper dishMapper;
	@Autowired
	private CategoryMapper categoryMapper;
	@Autowired
	private DishFlavorMapper dishFlavorMapper;
	
	@Override
	public Page<Dish> findByPage(String name, Integer pageNum, Integer pageSize) {
		//菜品中根据name进行分页查询
		
		//准备分页对象
		Page<Dish> page = new Page<>(pageNum, pageSize);
		
		//准备封装查询条件
		LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
		wrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);//如果name不为空,则根据name查询Dish的name
		
		page = dishMapper.selectPage(page, wrapper);//根据分页对象查询
		
		List<Dish> dishList = page.getRecords();
		
		if (CollectionUtil.isNotEmpty(dishList)) {
			for (Dish dish : dishList) {
				Category category = categoryMapper.selectById(dish.getCategoryId());
				dish.setCategoryName(category.getName());
			}
		}
		
		
		//遍历菜品名
		return page;
	}
	
	
	@Override
	public List<Dish> searchDishByName(String name) {
		LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
		wrapper.like(Dish::getName,name)
				.eq(Dish::getStatus,1);//菜品需要上架
		
		return dishMapper.selectList(wrapper);
	}
	
	@Override
	public List<Dish> findByCategoryId(Long categoryId, Long status) {
		//本次查询结果需要查两个表,一个是菜品表,一个菜品口味表,根据菜品id查出菜品信息,根据菜品id查出菜品口味
		LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
		
		//从Dish表中获取到对应id和对应状态的Dish
		wrapper.eq(Dish::getCategoryId,categoryId)
				.eq(Dish::getStatus, status);
		//菜品集合
		List<Dish> dishList = dishMapper.selectList(wrapper);
		if (CollectionUtil.isNotEmpty(dishList)) {
			for (Dish dish : dishList) {
				//根据菜品id从口味表中查询菜品口味信息
				//SQL   :   select * from dish_flavor where dish_id = #{dish.id}
				LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
				wrapper1.eq(DishFlavor::getDishId,dish.getId());
				List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
				
				//将菜品口味放入菜品对象中
				dish.setFlavors(dishFlavors);
			}
		}
		return dishList;
	}
	
	@Override
	public List<Dish> findByCategoryId(Long categoryId) {
		LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(Dish::getCategoryId,categoryId)
				.eq(Dish::getStatus,1);//菜品需要上架
		
		return dishMapper.selectList(wrapper);
	}
	
	@Override
	public void delete(List<String> ids) {
		if (ids != null && ids.size() > 0) {
			
			List<Dish> dishs = dishMapper.selectBatchIds(ids);
			//判断菜品中是否有未停售的菜品,如果有则直接抛出自定义删除异常
			for (Dish dish : dishs) {
				if (dish.getStatus() == Dish.DISH_STATUS_ENABLE) {
					throw new DeleteDishException("删除菜单中有未停售的菜品");
				}
			}
			
			//删除菜品
			dishMapper.deleteBatchIds(ids);
			//删除菜品口味
			dishFlavorMapper.deleteBatchIds(ids);
			
		}
	}
	
	@Override
	public void startAndStop(Integer statu, List<String> ids) {
		for (String id : ids) {
			UpdateWrapper<Dish> wrapper = new UpdateWrapper<>();
			wrapper.set("status", statu)
					.eq("id", id);
			
			dishMapper.update(null, wrapper);
		}
	}
	
	@Override
	public void update(Dish dish) {
		//更新菜品信息步骤
		
		// 更新菜品基本表信息
		dishMapper.updateById(dish);
		
		//删除口味表菜品相关信息
		
		LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(DishFlavor::getDishId, dish.getId());
		dishFlavorMapper.delete(wrapper);
		
		
		//将修改后的菜品口味重新保存到口味表中
		List<DishFlavor> flavors = dish.getFlavors();
		
		if (CollectionUtil.isNotEmpty(flavors)) {
			for (DishFlavor flavor : flavors) {
				flavor.setDishId(dish.getId());
				dishFlavorMapper.insert(flavor);
			}
		}
	}
	
	@Override
	public Dish findById(Long id) {
		//从菜品中根据id查询
		Dish dish = dishMapper.selectById(id);
		
		//根据菜品id从分类表中查询分类信息
		Category category = categoryMapper.selectById(dish.getCategoryId());
		dish.setCategoryName(category.getName());
		
		//根据菜品id从口味表中查询当前菜品口味列表
		LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
		
		//封装查询对象
		wrapper.eq(DishFlavor::getDishId, dish.getId());
		List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper);
		
		dish.setFlavors(dishFlavors);
		return dish;
	}
	
	@Override
	public void save(Dish dish) {
		//向dish表中保存基本信息
		dish.setCreateTime(new Date());
		dish.setUpdateTime(new Date());
		dishMapper.insert(dish);
		log.info("保存好的菜品主键为{}", dish.getId());
		
		//	遍历前端传入的口味信息表
		// 	LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
		// 	wrapper.eq(Category::getType,type);
		//
		// 	//查询出查询结果,返回查询列表
		// 	return categoryMapper.selectList(wrapper);
		
		
		List<DishFlavor> flavorList = dish.getFlavors();
		if (CollectionUtil.isNotEmpty(flavorList)) {
			for (DishFlavor dishFlavor : flavorList) {
				dishFlavor.setDishId(dish.getId());
				
				dishFlavorMapper.insert(dishFlavor);
			}
		}
	}
}
